package cn.itcast.hotel.utils;

import cn.itcast.hotel.pojo.dto.HotelDocDto;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;

/**
 * 搜索条件添加工具类
 *
 * @Author XiaoJiangShi
 * @Date 2022/8/8 20:15
 * @Blog blog.XiaoJiangShi.net/996.mba
 * @Description: 搜索条件添加工具类
 */
public class SearchConditionUtil {
    /**
     * 添加条件，进行筛选查询
     *
     * @param request     request对象
     * @param hotelDocDto 筛选参数对象
     */
    public static void boolSearch(SearchRequest request, HotelDocDto hotelDocDto) {
        BoolQueryBuilder builder = new BoolQueryBuilder();
        if (hotelDocDto.getKey() != null && "".equals(hotelDocDto.getKey())) {
            builder.must(QueryBuilders.matchQuery("all", hotelDocDto.getKey()));
            //搜索框高亮显示
            request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
        } else {
            builder.must(QueryBuilders.matchAllQuery());
        }

        if (hotelDocDto.getBrand() != null && "".equals(hotelDocDto.getBrand())) {
            builder.filter(QueryBuilders.termQuery("brand", hotelDocDto.getBrand()));
        }

        if (hotelDocDto.getCity() != null && "".equals(hotelDocDto.getCity())) {
            builder.filter(QueryBuilders.termQuery("city", hotelDocDto.getCity()));
        }

        if (hotelDocDto.getStarName() != null && "".equals(hotelDocDto.getStarName())) {
            builder.filter(QueryBuilders.termQuery("starName", hotelDocDto.getStarName()));
        }

        if (hotelDocDto.getMinPrice() != null && hotelDocDto.getMaxPrice() != null) {
            builder.filter(QueryBuilders.rangeQuery("price").gte(hotelDocDto.getMinPrice()).lte(hotelDocDto.getMaxPrice()));
        }

        FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(builder,
                new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
                        new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.termQuery("isAD", true)
                                , ScoreFunctionBuilders.weightFactorFunction(100))
                }).scoreMode(FunctionScoreQuery.ScoreMode.SUM);

        request.source().query(functionScoreQuery);
    }

    /**
     * 桶聚合条件
     *
     * @param request    request对象
     * @param aggName    聚合名称
     * @param filterName 聚合字段
     * @param size       聚合后获取条数
     */
    public static void addAgg(SearchRequest request, String aggName, String filterName, Integer size) {

        request.source().size(0)
                .aggregation(AggregationBuilders
                        .terms(aggName)
                        .field(filterName)
                        .size(size));

    }
}

